package com.woparker.kiosk.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.woparker.kiosk.R;
import com.woparker.kiosk.activity.NavigationActivity;
import com.woparker.kiosk.bean.SlotDTO;
import com.woparker.kiosk.utils.CommonUtils;
import com.woparker.kiosk.utils.DialogUtil;
import com.woparker.kiosk.utils.GlideUtilsKt;

import java.util.ArrayList;
import java.util.List;


public class LotsAdapter extends RecyclerView.Adapter<LotsAdapter.ViewHolder> {

    private List<SlotDTO> mItems = new ArrayList<SlotDTO>();

    private Context context;
    public LotsAdapter(Context context,List<SlotDTO> items) {
        this.context = context;
        mItems = items;
    }


    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_car_lot, parent, false);
        ViewHolder viewHolder = new ViewHolder(view);
        viewHolder.lotIv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int position = -1;
                try {
                    position = (int) viewHolder.itemView.getTag();
                } catch (Exception e) {
                }
                if (position < 0) {
                    return;
                }
                SlotDTO item = mItems.get(position);
                DialogUtil.showDialogPictrue(context, item);
            }
        });
        viewHolder.queryLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int position = -1;
                try {
                    position = (int) viewHolder.itemView.getTag();
                } catch (Exception e) {
                }
                if (position < 0) {
                    return;
                }
                SlotDTO item = mItems.get(position);
                Bundle bundle = new Bundle();
                bundle.putSerializable("SlotDTO", item);
                Intent intent = new Intent(context, NavigationActivity.class);
                intent.putExtras(bundle);
                context.startActivity(intent);
            }
        });
        return viewHolder;
    }


    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        SlotDTO item = mItems.get(position);
        if (item == null) {
            return;
        }
        //设置tag
        holder.itemView.setTag(position);
        GlideUtilsKt.loadImageByUrl(holder.lotIv, item.getPic());
        holder.plateTv.setText(item.getPlate());
        holder.textTime.setText(CommonUtils.getTime(item.getDate()));
        StringBuilder locationStr = new StringBuilder();
        if(!TextUtils.isEmpty(item.getPlaceName())){
            locationStr.append(item.getPlaceName());
            locationStr.append("层");
        }
        if(!TextUtils.isEmpty(item.getArea())){
            locationStr.append(" ");
            locationStr.append(item.getArea());
            locationStr.append("区");
        }
        if(!TextUtils.isEmpty(item.getName())){
            locationStr.append(" ");
            locationStr.append(item.getName());
            locationStr.append("车位");
        }
        if(!TextUtils.isEmpty(locationStr.toString())){
            holder.locationTv.setText(locationStr.toString());
        }
        else{
            holder.locationTv.setText("");
        }
    }

    @Override
    public int getItemCount() {
        return mItems != null ? mItems.size() : 0;
    }


    public class ViewHolder extends RecyclerView.ViewHolder {

        ImageView lotIv;
        TextView plateTv;
        TextView locationTv;
        TextView textTime;
        RelativeLayout queryLayout;

        public ViewHolder(View itemView) {
            super(itemView);
            lotIv = itemView.findViewById(R.id.plate_img);
            plateTv = itemView.findViewById(R.id.plate_stop_time);
            locationTv = itemView.findViewById(R.id.text_location);
            queryLayout = itemView.findViewById(R.id.query_l);
            textTime = itemView.findViewById(R.id.text_time);
        }
    }

}
